package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcAlbum;
import com.visizen.wisdomclass.server.model.WcAlbumImage;
import com.visizen.wisdomclass.server.model.WcAlbumImageExample;
import com.visizen.wisdomclass.server.model.WcAttachment;
import com.visizen.wisdomclass.server.model.dto.AlbumImageExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface WcAlbumImageMapper {
    /**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@SelectProvider(type = WcAlbumImageSqlProvider.class, method = "countByExample")
	int countByExample(WcAlbumImageExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@DeleteProvider(type = WcAlbumImageSqlProvider.class, method = "deleteByExample")
	int deleteByExample(WcAlbumImageExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@Delete({ "delete from wc_album_image",
			"where image_id = #{image_id,jdbcType=BIGINT}" })
	int deleteByPrimaryKey(Long image_id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@Insert({
			"insert into wc_album_image (image_id, album_id, ",
			"attach_id, user_id, ",
			"uploadtime)",
			"values (#{image_id,jdbcType=BIGINT}, #{album_id,jdbcType=BIGINT}, ",
			"#{attach_id,jdbcType=BIGINT}, #{user_id,jdbcType=BIGINT}, ",
			"#{uploadtime,jdbcType=TIMESTAMP})" })
	int insert(WcAlbumImage record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@InsertProvider(type = WcAlbumImageSqlProvider.class, method = "insertSelective")
	int insertSelective(WcAlbumImage record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@SelectProvider(type = WcAlbumImageSqlProvider.class, method = "selectByExample")
	@Results({
			@Result(column = "image_id", property = "image_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "album_id", property = "album_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "uploadtime", property = "uploadtime", jdbcType = JdbcType.TIMESTAMP) })
	List<WcAlbumImage> selectByExample(WcAlbumImageExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@Select({ "select", "image_id, album_id, attach_id, user_id, uploadtime",
			"from wc_album_image",
			"where image_id = #{image_id,jdbcType=BIGINT}" })
	@Results({
			@Result(column = "image_id", property = "image_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "album_id", property = "album_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "uploadtime", property = "uploadtime", jdbcType = JdbcType.TIMESTAMP) })
	WcAlbumImage selectByPrimaryKey(Long image_id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcAlbumImageSqlProvider.class, method = "updateByExampleSelective")
	int updateByExampleSelective(@Param("record") WcAlbumImage record,
			@Param("example") WcAlbumImageExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcAlbumImageSqlProvider.class, method = "updateByExample")
	int updateByExample(@Param("record") WcAlbumImage record,
			@Param("example") WcAlbumImageExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcAlbumImageSqlProvider.class, method = "updateByPrimaryKeySelective")
	int updateByPrimaryKeySelective(WcAlbumImage record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_album_image
	 * @mbggenerated
	 */
	@Update({ "update wc_album_image",
			"set album_id = #{album_id,jdbcType=BIGINT},",
			"attach_id = #{attach_id,jdbcType=BIGINT},",
			"user_id = #{user_id,jdbcType=BIGINT},",
			"uploadtime = #{uploadtime,jdbcType=TIMESTAMP}",
			"where image_id = #{image_id,jdbcType=BIGINT}" })
	int updateByPrimaryKey(WcAlbumImage record);

	/**
     * 通过班级ID查询新上传的张照
     * get latest image by classID
     *
     * @param classId
     * @return
     */
    @Select("SELECT * " +
            "FROM wc_album_image " +
            "WHERE album_id in (select album_id from wc_album where organization_id=#{classId}) " +
            "order by uploadtime desc " +
            "limit 1;")
    @Results({
            @Result(property = "album", column = "album_id", one = @One(select = "selectAlbumById")),
            @Result(property = "attachment", column = "attach_id", one = @One(select = "selectAttachById"))
    }
    )
    AlbumImageExt selectLastImagesByClassId(@Param("classId") long classId);

    /**
     * 通过attachmentId获取附件信息
     * select attachment by attachmentId
     *
     * @param attachId
     * @return
     */
    @Select("SELECT * FROM wc_attachment WHERE attach_id=#{attachId}")
    WcAttachment selectAttachById(@Param("attachId") long attachId);

    /**
     * 通过相册Id获取相册信息
     * select album by albumId
     *
     * @param albumId
     * @return
     */
    @Select("SELECT * FROM wc_album WHERE album_id=#{albumId}")
    WcAlbum selectAlbumById(@Param("albumId") long albumId);

    /**
     * 通过相册Id获取相册有照
     * select all images by albumId
     *
     * @param albumId
     * @param start
     * @param end
     * @return
     */
    @Select("SELECT * FROM wc_album_image WHERE album_id=#{albumId} LIMIT #{start},#{end}")
    List<WcAlbumImage> selectImagesByAlbumId(@Param("albumId") long albumId, @Param("start") int start, @Param("end") int end);

    /**
     * 通过相册Id删除?有相册中的照?
     *
     * @param albumId
     * @return
     */
    @Delete("DELETE FROM wc_album_image WHERE album_id=#{albumId}")
    int deleteByAlbumId(@Param("albumId") long albumId);

    /**
     * 查询是不是此用户上传的照片
     *
     * @param imageId
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM wc_album_image WHERE image_id=#{imageId} and user_id=#{userId}")
    int selectByImageIdAndUserId(@Param("imageId") long imageId, @Param("userId") long userId);

    @Select({
            "select * from wc_album_image ai",
            "where ai.user_id = #{userId}",
            "and ai.album_id = #{albumId}",
            "limit #{start}, #{count}"
    })
    List<WcAlbumImage> getUserPhotos(@Param("userId") Long userId,
                                     @Param("albumId") Long albumId,
                                     @Param("start") Integer start,
                                     @Param("count") Integer count);
}